<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订车人手机号" prop="ordererPhone">
        <el-input
          v-model="queryParams.ordererPhone"
          placeholder="请输入订车人手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机手机号" prop="driverPhone">
        <el-input
          v-model="queryParams.driverPhone"
          placeholder="请输入司机手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交时间">
        <el-date-picker
          v-model="daterangeSubmitTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['orders:bill:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['orders:bill:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['orders:bill:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['orders:bill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="疑义账单ID" align="center" prop="billId" />
      <el-table-column label="申诉单号" align="center" prop="appealNumber" />
      <el-table-column label="订单号" align="center" prop="orderNumber" />
      <el-table-column label="订车人" align="center" prop="ordererName" />
      <el-table-column label="订车人手机号" align="center" prop="ordererPhone" />
      <el-table-column label="司机" align="center" prop="driverName" />
      <el-table-column label="司机手机号" align="center" prop="driverPhone" />
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="疑义原因" align="center" prop="doubtReason">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.doubtful_type" :value="scope.row.doubtReason"/>
        </template>
      </el-table-column>
      <el-table-column label="处理类型" align="center" prop="handleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_doubtful_dispose" :value="scope.row.handleType"/>
        </template>
      </el-table-column>
      <el-table-column label="处理结果" align="center" prop="handleResult" />
      <el-table-column label="处理方案" align="center" prop="handleSolution" />
      <el-table-column label="调账前金额" align="center" prop="amountBeforeAdjust" />
      <el-table-column label="调账后金额" align="center" prop="amountAfterAdjust" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_doubtful_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="center" prop="operateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="operator" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['orders:bill:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['orders:bill:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改疑义账单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申诉单号" prop="appealNumber">
          <el-input v-model="form.appealNumber" placeholder="请输入申诉单号" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="订车人" prop="ordererName">
          <el-input v-model="form.ordererName" placeholder="请输入订车人" />
        </el-form-item>
        <el-form-item label="订车人手机号" prop="ordererPhone">
          <el-input v-model="form.ordererPhone" placeholder="请输入订车人手机号" />
        </el-form-item>
        <el-form-item label="司机" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入司机" />
        </el-form-item>
        <el-form-item label="司机手机号" prop="driverPhone">
          <el-input v-model="form.driverPhone" placeholder="请输入司机手机号" />
        </el-form-item>
        <el-form-item label="提交时间" prop="submitTime">
          <el-date-picker clearable
            v-model="form.submitTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择提交时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="疑义原因" prop="doubtReason">
          <el-select v-model="form.doubtReason" placeholder="请选择疑义原因">
            <el-option
              v-for="dict in dict.type.doubtful_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理类型" prop="handleType">
          <el-select v-model="form.handleType" placeholder="请选择处理类型">
            <el-option
              v-for="dict in dict.type.t_doubtful_dispose"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理结果" prop="handleResult">
          <el-input v-model="form.handleResult" placeholder="请输入处理结果" />
        </el-form-item>
        <el-form-item label="处理方案" prop="handleSolution">
          <el-input v-model="form.handleSolution" placeholder="请输入处理方案" />
        </el-form-item>
        <el-form-item label="调账前金额" prop="amountBeforeAdjust">
          <el-input v-model="form.amountBeforeAdjust" placeholder="请输入调账前金额" />
        </el-form-item>
        <el-form-item label="调账后金额" prop="amountAfterAdjust">
          <el-input v-model="form.amountAfterAdjust" placeholder="请输入调账后金额" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.t_doubtful_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间" prop="operateTime">
          <el-date-picker clearable
            v-model="form.operateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作人" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入操作人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBill, getBill, delBill, addBill, updateBill } from "@/api/orders/bill"

export default {
  name: "Bill",
  dicts: ['t_doubtful_dispose', 'doubtful_type', 't_doubtful_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 疑义账单表格数据
      billList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 操作人时间范围
      daterangeSubmitTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        ordererPhone: null,
        driverPhone: null,
        submitTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNumber: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        ordererName: [
          { required: true, message: "订车人不能为空", trigger: "blur" }
        ],
        ordererPhone: [
          { required: true, message: "订车人手机号不能为空", trigger: "blur" }
        ],
        submitTime: [
          { required: true, message: "提交时间不能为空", trigger: "blur" }
        ],
        doubtReason: [
          { required: true, message: "疑义原因不能为空", trigger: "change" }
        ],
        handleType: [
          { required: true, message: "处理类型不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        operateTime: [
          { required: true, message: "操作时间不能为空", trigger: "blur" }
        ],
        operator: [
          { required: true, message: "操作人不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询疑义账单列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeSubmitTime && '' != this.daterangeSubmitTime) {
        this.queryParams.params["beginSubmitTime"] = this.daterangeSubmitTime[0]
        this.queryParams.params["endSubmitTime"] = this.daterangeSubmitTime[1]
      }
      listBill(this.queryParams).then(response => {
        this.billList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        billId: null,
        appealNumber: null,
        orderNumber: null,
        ordererName: null,
        ordererPhone: null,
        driverName: null,
        driverPhone: null,
        submitTime: null,
        doubtReason: null,
        handleType: null,
        handleResult: null,
        handleSolution: null,
        amountBeforeAdjust: null,
        amountAfterAdjust: null,
        status: null,
        operateTime: null,
        operator: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSubmitTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.billId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加疑义账单"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const billId = row.billId || this.ids
      getBill(billId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改疑义账单"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.billId != null) {
            updateBill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addBill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.billId || this.ids
      this.$modal.confirm('是否确认删除疑义账单编号为"' + billIds + '"的数据项？').then(function() {
        return delBill(billIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('orders/bill/export', {
        ...this.queryParams
      }, `bill_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
